<template>
  <el-button @click="beginAdd()" type="danger">+新增</el-button>
  <el-button @click="save()" type="danger">保存</el-button>
  <el-button @click="cancel()" type="danger">撤销</el-button>
  <el-button @click="delBegin()" type="danger">删除</el-button>
  
  <h3>基本信息</h3>
  <br>
    <el-row type="flex" justify="" style="margin-left: 50px;" >
        <el-col :span="8">
          耳号: <el-input v-model="form.earNumber" placeholder="请输入耳号" style="width: 200px;"></el-input>
        </el-col>

        <el-col :span="8" style="margin-left: -37px;" >
          电子耳标号: <el-input v-model="form.electronicEarTag" placeholder="请输入电子耳标号" style="width: 195px;"></el-input>
        </el-col>

 
        <el-col :span="8">
          父亲耳号: <el-select
            v-model="form.farEarNumber"
            placeholder="请选择父亲耳号"
            style="width: 200px">
            <el-option
              v-for="item in proFatherEarData"
              :key="item.sleepInforId"
              :label="item.earNumber"
              :value="item.earNumber"
            /> </el-select>
        </el-col>

    </el-row>

  
    <el-row type="flex" justify="" style="margin-top: 20px;margin-left: 20px;" >

      <el-col :span="8">
          母亲耳号: <el-select
            v-model="form.moEarNumber"
            placeholder="请选择母亲耳号"
            style="width: 200px">
            <el-option
              v-for="item in proMotherEarData"
              :key="item.sleepInforId"
              :label="item.earNumber"
              :value="item.earNumber"
            /> </el-select>
        </el-col>

        <el-col :span="8" style="margin-left: 33px;">
          品种:<el-select
            v-model="form.varietyId"
            placeholder="请选择品种"
            style="width: 200px">
            <el-option
              v-for="item in varietyData"
              :key="item.varietyId"
              :label="item.varietyName"
              :value="item.varietyId"
            /> </el-select>
        </el-col>

        <el-col :span="8" style="margin-left: -35px;" >
          性别:<el-select
            v-model="form.gender"
            placeholder="请选择性别"
            style="width: 200px">
            <el-option
              label="男"
              :value="1"
            /> <el-option
              label="女"
              :value="2"
            /></el-select>
        </el-col>


    </el-row>
    
    <el-row type="flex" justify="" style="margin-left: 20px;margin-top: 20px;" >
        <el-col :span="8">
          出生状态: <el-select
            v-model="form.birthdayId"
            placeholder="请选择出生状态"
            style="width: 200px">
            <el-option
              v-for="item in birthdayData"
              :key="item.birthdayId"
              :label="item.birsdayName"
              :value="item.birthdayId"
            /> </el-select>
        </el-col>

        <el-col :span="8">
          出生日期:       
          <el-date-picker
        v-model="form.sleepBirthDate"
        type="date"
        placeholder="请选择出生日期"
      />
        </el-col>

        <el-col :span="8">
          入场日期:       
          <el-date-picker
        v-model="form.sleepEntryDate"
        type="date"
        placeholder="请选择入场日期"
      />
        </el-col>
    </el-row>

    <el-row type="flex" justify="" style="margin-top: 20px;margin-left: 20px;" >

        <el-col :span="8">
          出生场地: <el-input v-model="form.sleepBirthPlace"  style="width: 300px;"></el-input>
        </el-col>

        <el-col :span="8">
          出生重量: <el-input v-model="form.sleepBirthWeight" style="width: 300px;"></el-input>
        </el-col>

        <el-col :span="8">
          基因等级: <el-select
            v-model="form.gradeId"
            placeholder="请选择基因等级"
            style="width: 200px">
            <el-option
              v-for="item in gradeData"
              :key="item.gradeId"
              :label="item.grade"
              :value="item.gradeId"
            /> </el-select>
        </el-col>
    </el-row>

    <el-row type="flex" justify="" style="margin-top: 20px;margin-left: 50px;" >
        <el-col :span="16">
          备注: <el-input
            type="textarea"
            :rows="2"
            v-model="form.sleepNotes"
            :autosize="{ minRows: 2, maxRows: 2}"
            placeholder="请输入备注信息"
            style="margin-left: 46px;margin-top: -15px;"
            >
          </el-input>
        </el-col>

    </el-row>
  
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
const route = useRoute();
const router = useRouter();
const form=reactive({
  earNumber:'',
  varietyId:'',
  gender:'',
  sleepTypeId:route.query.typeId,
  ElectronicEarTag:'',
  gradeId:'',
  farEarNumber:'',
  moEarNumber:'',
  sleepBirthDate:'',
  sleepEntryDate:'',
  sleepBirthPlace:'',
  sleepBirthWeight:'',
  sleepNotes:'',
  birthdayId:'',
  farmId:route.query.id
})
//基因等级下拉框
const gradeData:any=ref([])
const getGradeData=()=>{
  axios.get('http://localhost:5039/api/SleepBegin/SearchGene').then(res=>{
    console.log('基因等级',res)
    gradeData.value=res.data.data
  })
}
//父母耳号下拉框
const proFatherEarData:any=ref([])
const getFatherEar=()=>{
  axios.get('http://localhost:5039/api/SleepBegin/SelectEarMale').then(res=>{
    console.log('父亲耳号',res)
    proFatherEarData.value=res.data.data
  })
}
const proMotherEarData:any=ref([])
const getMotherEar=()=>{
  axios.get('http://localhost:5039/api/SleepBegin/SelectEarFeMale').then(res=>{
    console.log('母亲耳号',res)
    proMotherEarData.value=res.data.data
  })
}

//品种下拉框
const varietyData:any=ref([])
const getvarietyData=()=>{
  axios.get('http://localhost:5039/api/SleepBegin/SearchVariety').then(res=>{
    console.log('品种',res)
    varietyData.value=res.data.data
  })
}

//出生状态
const birthdayData:any=ref([])
const getBirthdayData=()=>{
  axios.get('http://localhost:5039/api/SleepBegin/SearchBirth').then(res=>{
    console.log('出生状态',res)
    birthdayData.value=res.data.data
  })
}
//添加
const save=()=>{
  axios.post('http://localhost:5039/api/SleepBegin/Handle/SheepInforAdd',form).then(res=>{
     console.log('公母档案添加',res)
     if(res.data.code>0){
      ElMessage.success('添加成功')
      router.push({
        name:'RecordView'
      })
     }
     if(res.data.code==-1){
      ElMessage.error('该耳号已存在')
     }
  })
}


onMounted(()=>{
  getGradeData()
  getFatherEar()
  getMotherEar()
  getvarietyData()
  getBirthdayData()
})
</script>